<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>state练习-点击修改元素背景颜色</title>
    <style>
        #el{
            width: 300px;
            height: 200px;
            border:solid 1px #ddd;
        }

    </style>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>

    <script type="text/babel">
        class App extends React.Component{
            //1. 声明state 属性, 并设置 bg
            state = {
                bg: '#000'
            }

            render(){
                //2. 获取颜色的值  设置给 background
                let {bg} = this.state;
                return <div style={{background: bg}} id="el" onClick={this.click}></div>
            }

            click = () => {
                //3. 获取随机颜色  _.random 生成随机数  
                let newColor = `rgb(${_.random(0,100)},${_.random(0,100)},${_.random(0,100)})`;
                //4. 修改 bg 的值
                this.setState({
                    bg: newColor
                })
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>
</html>